package cn.jf.modules.system.service;

import cn.jf.modules.system.entity.SysRole;
import cn.jf.modules.system.entity.SysRolePermission;
import cn.jf.modules.system.mapper.SysRoleMapper;
import cn.jf.modules.system.mapper.SysRolePermissionMapper;
import cn.jf.modules.system.mapper.SysUserRoleMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @Author scott
 * @since 2018-12-19
 */
@Service(version = "1.0.0")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysUserRoleMapper userRoleMapper;
    @Autowired
    private SysRolePermissionMapper rolePermissionMapper;

    public IPage<SysRole> getPageList(IPage<SysRole> page, SysRole sysRole) {
        return page.setRecords(this.baseMapper.getPageList(page,sysRole));
    }

    @Override
    public SysRole getByRoleCode(String roleCode) {
        return this.baseMapper.getByRoleCode(roleCode);
    }

    @Override
    public List<SysRole> getList(SysRole sysRole) {
        return this.baseMapper.getList(sysRole);
    }
    @Transactional
    public void deleteRole(List<String> ids){
          for (String id:ids) {
              this.baseMapper.deleteById(id);
              userRoleMapper.deltUserRoleByRoleId(id);
              SysRolePermission sysRolePermission = new SysRolePermission();
              sysRolePermission.setRoleId(id);
              rolePermissionMapper.deleteByEntity(sysRolePermission);
          }
    }
}
